package gridView;

import gridModel.GridModel;
import gridModel.NamedColor;
import gridModel.PixStates;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JComponent;

public class Pix extends JComponent {

	private static final long serialVersionUID = 1L;
	
	private final GridModel model;
	private final Point coord;
	private final NamedColor color;
	
	public Pix(int x, int y, PixSizes size, GridModel m, NamedColor c) {
		model = m;
		color = c;
		coord = new Point(x, y);
		setPreferredSize(new Dimension(size.getPixSize(), size.getPixSize()));
	}
	
	public void paintComponent(Graphics g) {
		int start_x = 0,
			start_y = 0;
		int w = getWidth(),
			h = getHeight();
		
		// Bordure classique
		// A gauche
		if (coord.x == 0 || coord.x % GridConstants.HELP_RHYTHM != 0) {
			g.setColor(GridConstants.BORDER_COLOR.getValue());
			g.fillRect(0, 0, GridConstants.BORDER_WIDTH, getHeight());
			start_x += GridConstants.BORDER_WIDTH;
			w -= GridConstants.BORDER_WIDTH;
		}
		// En haut
		if (coord.y == 0 || coord.y % GridConstants.HELP_RHYTHM != 0) {
			g.setColor(GridConstants.BORDER_COLOR.getValue());
			g.fillRect(0, 0, getWidth(), GridConstants.BORDER_WIDTH);
			start_y += GridConstants.BORDER_WIDTH;
			h -= GridConstants.BORDER_WIDTH;
		}
		// A droite
		if (coord.x == model.getWidth() - 1) {
			g.setColor(GridConstants.BORDER_COLOR.getValue());
			g.fillRect(getWidth() - GridConstants.BORDER_WIDTH, 0, GridConstants.BORDER_WIDTH, getHeight());
			w -= GridConstants.BORDER_WIDTH;
		}
		// En bas
		if (coord.y == model.getHeight() - 1) {
			g.setColor(GridConstants.BORDER_COLOR.getValue());
			g.fillRect(0, getHeight() - GridConstants.BORDER_WIDTH, getWidth(), GridConstants.BORDER_WIDTH);
			h -= GridConstants.BORDER_WIDTH;
		}
		
		// Bordure help
		// A gauche
		if (coord.x != 0 && coord.x % GridConstants.HELP_RHYTHM == 0) {
			g.setColor(GridConstants.HELP_SEE_COLOR.getValue());
			g.fillRect(0, 0, GridConstants.HELP_SEE_WIDTH, getHeight());
			start_x += GridConstants.HELP_SEE_WIDTH;
			w -= GridConstants.HELP_SEE_WIDTH;
		}
		// En haut
		if (coord.y != 0 && coord.y % GridConstants.HELP_RHYTHM == 0) {
			g.setColor(GridConstants.HELP_SEE_COLOR.getValue());
			g.fillRect(0, 0, getWidth(), GridConstants.HELP_SEE_WIDTH);
			start_y += GridConstants.HELP_SEE_WIDTH;
			h -= GridConstants.HELP_SEE_WIDTH;
		}
		
		// Contenu
		NamedColor c = model.thereIs(coord.x, coord.y, PixStates.FULL);
		if (c != null) {
			g.setColor(c.getValue());
			g.fillRect(start_x, start_y, start_x + w, start_y + h);
			if (c.isWhite()) {
				g.setColor(c.getThinColor());
				g.drawRect(start_x, start_y, w - 1, h - 1);
			}
		} else if (model.getPixState(coord.x, coord.y).equals(PixStates.CROSSED)) {
			g.setColor(color.getThinColor());
			g.drawLine(start_x, start_y, start_x + w, start_y + h);
			g.drawLine(start_x, start_y + h, start_x + w, start_y);
		}
	}

}
